<template>
  <div>
    <el-dialog :title="salesForm.salesId?'修改信息':'添加信息'" :visible.sync="visible">
      <el-form :model="salesForm" label-width="80px" :rules="rules" ref="salesDom">
        <el-form-item label="时间" prop="salesDate">
          <el-date-picker :editable="false" v-model="salesForm.salesDate" value-format="yyyy-MM-dd" placeholder="销售日期"/>
        </el-form-item>
        <el-form-item label="商品" prop="goodsId">
          <el-select v-model="salesForm.goodsId" clearable filterable placeholder="请输入选择商品">
            <el-option v-for="item in goodsList" :key="item.goodsId" :label="item.name" :value="item.goodsId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="客户名" prop="customerName">
          <el-input v-model="salesForm.customerName" clearable placeholder="客户名称"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="contactAddress">
          <el-input v-model="salesForm.contactAddress" clearable placeholder="客户地址"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="contactPhone">
          <el-input v-model="salesForm.contactPhone" clearable placeholder="客户电话"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="saleNum">
          <el-input v-model="salesForm.saleNum" :readonly="this.salesForm.salesId?true:false" clearable placeholder="商品数量"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="salesForm.status">
            <el-radio :label="0">异常</el-radio>
            <el-radio :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button @click="visible=false">取消</el-button>
          <el-button type="primary" @click="submitInfo">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
const initInfo = {
  salesId: '',
  salesDate: '',
  customerName: '',
  contactAddress: '',
  contactPhone: '',
  saleNum: '',
  status: 1,
  goodsId: ''
}

export default {
  name: 'SalesAddOrUpdate',
  data () {
    return {
      visible: false,
      salesForm: {
        ...initInfo
      },
      rules: {
        salesDate: [
          { required: true, message: '时间不能为空', trigger: 'blur' }
        ],
        goodsId: [
          { required: true, message: '商品不能为空', trigger: 'blur' }
        ],
        customerName: [
          { required: true, message: '客户名不能为空', trigger: 'blur' }
        ],
        contactAddress: [
          { required: true, message: '客户地址不能为空', trigger: 'blur' }
        ],
        contactPhone: [
          { required: true, message: '客户电话不能为空', trigger: 'blur' }
        ],
        saleNum: [
          { required: true, message: '数量不能为空', trigger: 'blur' }
        ]
      },
      goodsList: [],
      loading: ''
    }
  },
  methods: {
    init (salesId) {
      /* 开启弹窗 */
      this.visible = true
      this.salesForm = { ...initInfo }

      this.$http.get('/sales/select').then(data => {
        if (data.code === 0) {
          this.goodsList = data.data
        }
      })

      if (salesId) {
        /* 成立表示该次操作为修改，发送ajax请求获取信息 */
        this.$http.get('/sales/info/' + salesId).then(data => {
          if (data.code === 0) {
            this.salesForm = {
              ...data.sale
            }
          } else {
            this.$message.error('数据加载失败，请重试！')
          }
        })
      }
    },
    submitInfo () {
      this.$refs.salesDom.validate(valid => {
        if (valid) {
          this.loading = this.$loading({ text: '加载中...' })
          this.$http.post('/sales/update', this.salesForm).then(data => {
            this.loading.close()
            if (data.code === 0) {
              this.$message({
                message: `${!this.salesForm.salesId ? '添加' : '修改'}成功`,
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('onCloseWin')
                }
              })
            } else {
              this.$alert(data.msg, { type: 'error' })
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
